/**
 * 
 */
package com.aat.dxfy.thread.safe1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @date 2016年6月23日 下午1:36:02
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 功能是：
 * @info java spring
 * @update none
 */
public class Test03 {

	private static int count = 0;

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		final Add a1 = new Add(count);
		final Add a2 = new Add(count);
		service.submit(a1);
		service.submit(a2);
		
		final Sub s1 = new Sub(count);
		final Sub s2 = new Sub(count);
		service.submit(s1);
		service.submit(s2);

		Thread thread = new Thread() {
			public void run() {
				while (true) {
					System.out.println(a1.getCount()+" "+a2.getCount());
					count=a1.getCount()+a2.getCount();
					System.out.println("count " + count);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
					}
				}
			};
		};

		service.submit(thread);
		service.shutdown();
	}

}

class Add extends Thread {
	private int count;

	public Add(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void run() {
		while (true) {
			count++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}

}

class Sub extends Thread {
	private int count;

	public Sub(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void run() {
		while (true) {
			count--;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}

}
